package com.softwarerusher.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.softwarerusher.daoImpl.BookDAO;
import com.softwarerusher.model.TBook;

public class netshopsearch extends ActionSupport implements RequestAware,SessionAware{

    private  int requestpagenum ;
    private Map<String,Object> request;
    private Map<String,Object> session;
    private List<TBook> bookList;
    private int totalnum = 0 ;
	private String searchcontext;
	private static String search = "";
	private static int pagesize = 3;

	public String search()
	{
		search = searchcontext;
		//System.out.print(searchcontext);
		List<TBook> result = BookDAO.queryByName(searchcontext);
		//BooKDAOTest.printt(result);
		//System.out.print("dy"+result.size());
		if(result.size()==0)
		{
			request.put("context", searchcontext);
			return "none";
		}
		bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(1));
		return "success";
	}
	public String pagenum()
	{
		
		System.out.print("11");
		List<TBook> result = BookDAO.queryByName(search);
		
		System.out.print("dy"+result.size());


		bookList = BookDAO.paging(result, requestpagenum, pagesize);
    	if(result.size()%pagesize==0)
    	{
    	    totalnum = result.size()/pagesize;
    	}else{
    		totalnum = result.size()/pagesize+1;
    	}
    	session.put("totalnum", Integer.toString(totalnum));
    	session.put("currentpage", String.valueOf(requestpagenum));
		return "success";
	}


	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}
	public String getSearchcontext() {
		return searchcontext;
	}
	public void setSearchcontext(String searchcontext) {
		this.searchcontext = searchcontext;
	}
	public Map<String, Object> getRequest() {
		return request;
	}
	public int getRequestpagenum() {
		return requestpagenum;
	}
	public void setRequestpagenum(int requestpagenum) {
		this.requestpagenum = requestpagenum;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public List<TBook> getBookList() {
		return bookList;
	}
	public void setBookList(List<TBook> bookList) {
		this.bookList = bookList;
	}
	public int getTotalnum() {
		return totalnum;
	}
	public void setTotalnum(int totalnum) {
		this.totalnum = totalnum;
	}

	
	
}
